# -*- coding: utf-8 -*-

class View
  
  attr_accessor :list_view_button
  attr_accessor :list_edit_button
  attr_accessor :list_delete_button
  attr_accessor :script_name
  
  attr_accessor :ajax
  
  def initialize()
    initialize_values()
  end
  
  def initialize_values()
    @list_view_button = true
    @list_edit_button = true
    @list_delete_button = true
    @script_name = ""
    @ajax = false
  end
  
  def disable_list_button()
    #@list_view_button = false
    @list_edit_button = false
    @list_delete_button = false
  end
  
  def load(values, filename)
    
    #f = open(filename)
    #html = f.read
    #f.close
    
    #f = File.open(filename, :encoding => "UTF-8")
    f = File.open(filename, "r")
    html = f.read
    f.close
    
    values.each { |key, val|
      html = html.gsub("_%#{key}%_", val.to_s)
      #html += "#{key} #{val}<br>"
    }
    
    return html
    
  end
  
  def load_edit_form(values, filename)
    
    #f = open(filename)
    #html = f.read
    #f.close
    
    #f = File.open(filename, :encoding => "UTF-8")
    f = File.open(filename, "r")
    html = f.read
    f.close
    
    #html = IO.read(filename)
    
    values.each_pair { |key, val|
      #print "#{key}:#{val}<br />"
      html = html.gsub("_%#{key}%_", val["val"].to_s)
    }
    
    return html
    
  end
  
  def load_view_form(values, filename)
    return load_edit_form(values, filename)
  end
  
  def get_action_code()
    if @script_name != "" then
      html = " action=\"#{@script_name}\""
    else
      html = ""
    end
    return html
  end
  
  def make_find_form(name, word)
    
    word = (word.nil?)?"":word
=begin
    html = <<EOF
<table>
  <form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="find" />
  <input type="hidden" name="column" value="#{name}" />
  <tr>
    <td><input type="text" name="word" value="#{CGI.escapeHTML(word)}" /></td>
    <td><input type="submit" name="submit" value="#{$msg_view_search}" /></td>
  </tr>
  </form>
</table>
EOF
=end
    html = <<EOF
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="find" />
  <input type="hidden" name="column" value="#{name}" />
  <input type="text" name="word" value="#{CGI.escapeHTML(word)}" />
  <input type="submit" name="submit" value="#{$msg_view_search}" />
</form>
EOF
    return html
    
  end
  
  def get_find_form_with_option(name, word, option)
    word = (word.nil?)?"":word
    html = ""
    option.each {|key, val|
      html += "<input type='hidden' name='#{key}' value='#{val}' />"
    }
    html = <<EOF
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="find" />
  <input type="hidden" name="column" value="#{name}" />
  #{html}
  <input type="text" name="word" value="#{CGI.escapeHTML(word)}" />
  <input type="submit" name="submit" value="#{$msg_view_search}" />
</form>
EOF
   return html
  end
  
  def make_list_table(headers, fields, values)
    
    #html = get_new_form()
    html =""
    if values.length > 0 then
      html += get_list_table(headers, fields, values)
    end
    
    return html
    
  end
  
  def get_new_form()
  
    html = <<EOF
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="edit" />
  <input type="hidden" name="id" value="0" />
  <td><input type="submit" name="submit" value="#{$msg_view_new}" /></td>
</form>
EOF
    return html
    
  end
  
  def get_list_table(headers, fields, values)
  
    html = "<table class='list_table' border=0 cellpadding=3 cellspacing=0>"
    
    html += "<tr>"
    
    headers.each do |col|
      if col != "id" then
        html += "<th class='list_table_td'>#{col}</th>"
      end
    end
    
    html += "<th class='list_table_td'>&nbsp;</th></tr>"
    
    values.each do |row|
      
      html += "<tr>"
      
      #row.each do |key, val|
      fields.each do |fd|
        
        if fd != "id" then
          val = row[fd]
          if val == "" || val == nil then
            val = "&nbsp;"
          else
            #val = CGI.escapeHTML(val.to_s)
            #val = CGI.escapeHTML(val.to_s)
          end
          html += "<td class='list_table_td'>#{val}</td>"
        end
        
      end
      
      button = ""
      
      if @list_view_button then
        if @ajax == true then
          button += <<EOF
      <td>
      <form>
        <input type="hidden" name="mode" value="view" />
        <input type="hidden" name="type" value="#{@script_name}" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="button" name="submit" value="#{$msg_view_view}" onclick="update_contents_by_form('detail_view', this.form)" />
      </form>
      </td>
EOF
        else
          button += <<EOF
      <td>
      <form method="post"#{get_action_code()}>
        <input type="hidden" name="mode" value="view" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="submit" name="submit" value="#{$msg_view_view}" />
      </form>
      </td>
EOF
        end
      end
      
      if @list_edit_button then
        if @ajax == true then
          button += <<EOF
      <td>
      <form>
        <input type="hidden" name="mode" value="edit" />
        <input type="hidden" name="type" value="#{@script_name}" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="button" name="submit" value="#{$msg_view_edit}" onclick="update_contents_by_form('detail_view', this.form)" />
      </form>
      </td>
EOF
        else
          button += <<EOF
      <td>
      <form method="post"#{get_action_code()}>
        <input type="hidden" name="mode" value="edit" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="submit" name="submit" value="#{$msg_view_edit}" />
      </form>
      </td>
EOF
        end
      end
      
      if @list_delete_button then
        if @ajax == true then
          button += <<EOF
      <td>
      <form>
        <input type="hidden" name="mode" value="del_conf" />
        <input type="hidden" name="type" value="#{@script_name}" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="button" name="submit" value="#{$msg_view_delete}" onclick="update_contents_by_form('detail_view', this.form)" />
      </form>
      </td>
EOF
        else
          button += <<EOF
      <td>
      <form method="post"#{get_action_code()}>
        <input type="hidden" name="mode" value="del_conf" />
        <input type="hidden" name="id" value="#{row['id']}" />
        <input type="submit" name="submit" value="#{$msg_view_delete}" /></form></td>
EOF
        end
      end
      
      if button != "" then
        button = "<table border=0><tr>#{button}</tr></table>"
      else
        button = "&nbsp;"
      end
      
      html += "<td class='list_table_td'>#{button}</td>"
      
      html += "</tr>\n"
      
    end
    
    html += "</table>"
    
    return html
    
  end
  
  def make_view_form_with_edit(values)
    
    html = make_view_form(values)
    html += add_edit_button(values['id']['val'])
    return html
    
  end
  
  def add_edit_button(id)
    if @ajax == true then
      html = <<EOF
<form>
  <input type="hidden" name="mode" value="edit" />
  <input type="hidden" name="type" value="#{@script_name}" />
  <input type="hidden" name="id" value="#{id}" />
  <input type="button" name="submit" value="#{$msg_view_edit}" onclick="update_contents_by_form('detail_view', this.form)" />
</form>
EOF
    else
      html = <<EOF
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="edit" />
  <input type="hidden" name="id" value="#{id}" />
  <input type="submit" name="submit" value="#{$msg_view_edit}" />
</form>
EOF
    end
    return html
  end
  
  def make_view_form_with_delete(values)
    
    html = "<p style='color: #FF0000;'>#{$msg_view_delete_confirmation}</p>"
    html += make_view_form(values)
    html += add_delete_button(values['id']['val'])
    return html
    
  end
  
  def add_delete_button(id)
    if @ajax == true then
      html = <<EOF
<form>
  <input type="hidden" name="mode" value="delete" />
  <input type="hidden" name="type" value="#{@script_name}" />
  <input type="hidden" name="id" value="#{id}" />
  <input type="button" name="submit" value="#{$msg_view_edit}" onclick="update_contents_by_form('detail_view', this.form)" />
</form>
EOF
    else
      html = <<EOF
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="delete" />
  <input type="hidden" name="id" value="#{id}" />
  <input type="submit" name="submit" value="#{$msg_view_delete}" />
</form>
EOF
    end
    return html
  end
  
  def make_view_form(values)
    
    html = "<table>"
    
    values.each do |key, val|
      if key == "id" then
      
      else
        html += <<EOF
<tr>
  <td height="30px">#{val["caption"].to_s}</td>
  <td>#{CGI.escapeHTML(val["val"].to_s)}</td>
</tr>
EOF
      end
    end

    html += "</table>"
    
    return html
    
  end
  
  def make_edit_form(values)
    
    html = "<form method=\"post\"#{get_action_code()}><table>"
    html += "<input type=\"hidden\" name=\"mode\" value=\"conf\" />"
    values.each do |key, val|
      if key == "id" then
        html += "<input type=\"hidden\" name=\"#{key}\" value=\"#{val["val"]}\" />"
      elsif key == "submit" then
        html += ""
      elsif key == "mode" then
      
      else
        form = ""
        case val["type"]
          when "hidden"
            form = "<input type=\"hidden\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val["val"].to_s)}\" />"
          when "text"
            form = "<input type=\"text\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val["val"].to_s)}\" size=\"50\" />"
          when "textarea"
            form = "<textarea name=\"#{key}\">#{CGI.escapeHTML(val["val"].to_s)}</textarea>"
          else
            form = val["val"].to_s
        end
        
        caption = val["caption"]
        if val["type"] != "hidden" then
          html += <<EOF
<tr>
  <td height="30px">#{caption}</td>
  <td>#{form}</td>
</tr>
EOF
        else
          html += form
        end
      end
    end
    
    if @ajax then
      html += <<EOF
<tr>
  <td>&nbsp;</td>
  <td><input type="button" name="submit" value="#{$msg_view_submit}" onclick="update_contents_by_form('detail_view', this.form)" /></td>
</tr>
</table>
</form>
EOF
    else
      html += <<EOF
<tr>
  <td>&nbsp;</td>
  <td><input type="submit" name="submit" value="#{$msg_view_submit}" /></td>
</tr>
</table>
</form>
EOF
    end
    
    return html
    
  end
  
  def make_conf_form(values)
    
    html = "<table>"
    #form = ""
    
    values.each do |key, val|
      if key == "id" then
        #form += "<input type=\"hidden\" name=\"#{key}\" value=\"#{val}\" />"
        html += ""
      elsif key == "submit" then
        html += ""
      elsif key == "mode" then
        html += ""
      elsif val["type"] == "hidden" then
        html += "<input type=\"hidden\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val.to_s)}\" />"
      else
        #form += "<input type=\"hidden\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val.to_s)}\" />"
        html += <<EOF
<tr>
  <td height="30px">#{val["caption"]}</td>
  <td>#{val["val"]}</td>
</tr>\n
EOF

      end
    end
    
    html += <<EOF
<tr>
  <td>&nbsp;</td>
  <td>
    #{make_button_for_conf(values)}
  </td>
</tr>
EOF
    html += "</table>"
    
    return html
    
  end
  
  def make_conf_values(mode, values)
    
    form = "<input type=\"hidden\" name=\"mode\" value=\"#{mode}\" />"
    
    values.each do |key, val|
      if key == "id" then
        form += "<input type=\"hidden\" name=\"#{key}\" value=\"#{val["val"]}\" />"
      elsif key == "submit" then
        form += ""
      elsif key == "mode" then
        form += ""
      else
        form += "<input type=\"hidden\" name=\"#{key}\" value=\"#{CGI.escapeHTML(val["val"].to_s)}\" />"
      end
    end
    
    return form
    
  end
  
  def make_button_for_conf(values)
    
    if @ajax == true then
    
      html = <<EOF
<table>
  <tr>
    <td>
      <form method="post"#{get_action_code()}>
        #{make_conf_values("apply", values)}
        <input type="button" name="submit" value="#{$msg_view_submit}" onclick="update_contents_by_form('detail_view', this.form)" />
      </form>
    </td>
    <td>
      <form method="post"#{get_action_code()}>
        #{make_conf_values("reedit", values)}
        <input type="button" name="submit" value="#{$msg_view_edit}" onclick="update_contents_by_form('detail_view', this.form)" />
      </form>
    </td>
  </tr>
</table>
EOF
    else
    
      html = <<EOF
<table>
  <tr>
  <form method="post"#{get_action_code()}>#{make_conf_values("apply", values)}
    <td><input type="submit" name="submit" value="#{$msg_view_submit}" /></td>
  </form>
  <form method="post"#{get_action_code()}>#{make_conf_values("reedit", values)}
    <td><input type="submit" name="submit" value="#{$msg_view_edit}" /></td>
  </form>
  </tr>
</table>
EOF
    end
    
    return html
    
  end
  
  def make_apply_form(values)
    
    html = <<EOF
<table><tr><td>
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="list" />
  <input type="submit" name="submit" value="#{$msg_view_list}" />
</form>
</td><td>
<form method="post"#{get_action_code()}>
  <input type="hidden" name="mode" value="view" />
  <input type="hidden" name="id" value="#{values["id"]}" />
  <input type="submit" name="submit" value="#{$msg_view_view}" />
</form>
</td><td>
<form method="post">
  <input type="hidden" name="mode" value="edit" />
  <input type="hidden" name="id" value="0" />
  <input type="submit" name="submit" value="#{$msg_view_new}" />
</form>
</td></tr></table>
EOF
    
    return html
    
  end
  
end
